package org.aceor.mddal.common.druid;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
import org.aceor.mddal.gateway.sqlparser.visitor.AbstractMysqlVisitor;

/**
 * Created by lxue on 16/3/21.
 */
public class SimpleMysqlOutputVisitor extends AbstractMysqlVisitor {

    public SimpleMysqlOutputVisitor(Appendable appender) {
        super(appender);
    }

    @Override
    public boolean visit(SQLOrderBy sqlOrderBy) {
        if (null == sqlOrderBy || null == sqlOrderBy.getItems() || sqlOrderBy.getItems().isEmpty()) {
            System.out.println("又tm null");
            return super.visit(sqlOrderBy);
        }
        SQLExpr expr = sqlOrderBy.getItems().get(0).getExpr();
        if (expr instanceof SQLPropertyExpr) {
            System.out.println("order by type: SQLPropertyExpr, owner: "
                    +((SQLPropertyExpr)expr).getOwner()+", name: "
                    +((SQLPropertyExpr)expr).getName()+", simpleName: "
                    +((SQLPropertyExpr)expr).getSimpleName());
        } else if (expr instanceof SQLAllColumnExpr) {
            System.out.println("order by type: SQLAllColumnExpr");
        } else if (expr instanceof SQLIdentifierExpr) {
            System.out.println("order by type: SQLIdentifierExpr");
        } else if (expr instanceof SQLAggregateExpr) {
            System.out.println("order by type: SQLAggregateExpr, methodName: "+ ((SQLAggregateExpr)expr).getMethodName());
        } else {
            System.out.println("order by type: "+expr.getClass());
        }
        return super.visit(sqlOrderBy);
    }

    @Override
    public boolean visit(final MySqlSelectGroupByExpr groupByExpr) {
        if (null != groupByExpr.getType()) {
            System.out.println("group by type: "+groupByExpr.getType());
        }
        return super.visit(groupByExpr);
    }
}
